package com.jason.apps.buyer;

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.LoginActivity;
import com.apps.realtyportal.MyFragmentPagerAdapter;
import com.apps.realtyportal.R;
import com.ipaulpro.afilechooser.utils.FileUtils;
import com.jason.apps.fromdashboard.ForRent;
import com.jason.apps.fromdashboard.ForSale;
import com.shanu.apps.details.ShowPropertyDetail;

@SuppressLint({ "NewApi", "NewApi", "ParserError" })
public class Buyer extends SherlockFragmentActivity implements
		Favorites.OnClickSelectedListener, ForRent.RentOnClickSelectedListener,
		ForSale.SaleOnClickSelectedListener {

	public static String Path = null;
	public static String DATE = null;
	private static final int REQUEST_CODE = 1;
	private static final int ID_LOGOUT = 1;
	public static final int DASHBOARD = 2;

	public static Context appContext;
	public static ActionBar actionbar;
	FragmentManager fm;

	ViewPager mPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buyer_fragment);
		appContext = getApplicationContext();

		// ActionBar
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setDisplayShowTitleEnabled(false);
		/*
		 * actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		 * 
		 * ActionBar.Tab rent = actionbar.newTab().setText("For Rent")
		 * .setIcon(R.drawable.allproperty); ActionBar.Tab sale =
		 * actionbar.newTab().setText("For Sale")
		 * .setIcon(R.drawable.allproperty); ActionBar.Tab favorites =
		 * actionbar.newTab().setText("Favorites")
		 * .setIcon(R.drawable.favorites); ActionBar.Tab profile =
		 * actionbar.newTab().setText("Profile") .setIcon(R.drawable.profile);
		 * 
		 * Fragment rentFragment = new ForRent(); Fragment saleFragment = new
		 * ForSale(); Fragment EditFragment = new Favorites(); Fragment
		 * DeleteFragment = new Profile();
		 * 
		 * sale.setTabListener(new MyTabsListener(rentFragment));
		 * rent.setTabListener(new MyTabsListener(saleFragment));
		 * favorites.setTabListener(new MyTabsListener(EditFragment));
		 * profile.setTabListener(new MyTabsListener(DeleteFragment));
		 * 
		 * // actionbar.addTab(all); actionbar.addTab(rent);
		 * actionbar.addTab(sale); actionbar.addTab(favorites);
		 * actionbar.addTab(profile);
		 */

		/** Getting a reference to action bar of this activity */
		// actionbar = getSupportActionBar();

		/** Set tab navigation mode */
		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		/** Getting a reference to ViewPager from the layout */
		mPager = (ViewPager) findViewById(R.id.pager);

		/** Getting a reference to FragmentManager */
		fm = getSupportFragmentManager();

		/** Defining a listener for pageChange */
		ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				actionbar.setSelectedNavigationItem(position);
			}

		};

		/** Setting the pageChange listner to the viewPager */
		mPager.setOnPageChangeListener(pageChangeListener);

		/** Creating an instance of FragmentPagerAdapter */
		MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
				fm);

		/** Setting the FragmentPagerAdapter object to the viewPager object */
		mPager.setAdapter(fragmentPagerAdapter);

		actionbar.setDisplayShowTitleEnabled(true);

		/** Defining tab listener */
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			public Fragment fragment;

			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			}

			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mPager.setCurrentItem(tab.getPosition());
				// ft.replace(R.id.buyer_fragment, fragment);
			}

			public void onTabReselected(Tab tab, FragmentTransaction ft) {

				fm.popBackStack();
			}
		};

		/** Creating Android Tab */
		Tab tab = actionbar.newTab().setText("For Rent")
				.setIcon(R.drawable.mypropertyxml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("For Sale")
				.setIcon(R.drawable.forsalexml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("Favorites")
				.setIcon(R.drawable.favorite).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("Profile")
				.setIcon(R.drawable.profiles).setTabListener(tabListener);

		actionbar.addTab(tab);

	}

	public void BrowseLead(View v) {
		Intent target = FileUtils.createGetContentIntent();
		Intent intent = Intent.createChooser(target,
				getString(R.string.chooser_title));

		try {
			startActivityForResult(intent, REQUEST_CODE);
		} catch (ActivityNotFoundException e) {
		}
	}
	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

		// /* Add an expandable Search Icon in the ActionBar */
		// menu.add(0, ID_LOGOUT, 0, "Search")
		// .setIcon(R.drawable.logouticon)
		// .setShowAsAction(
		// MenuItem.SHOW_AS_ACTION_IF_ROOM);
		//
		//
		//
		// return super.onCreateOptionsMenu(menu);
		
		SubMenu subMenu = menu.addSubMenu("Options");

		ShowPropertyDetail details = (ShowPropertyDetail) getSupportFragmentManager()
				.findFragmentById(R.id.buyer_fragment);
		
		if(details!=null){
			
		 menu.add(0, DASHBOARD, 0, "Logout")
		 .setTitle("Favorites")
		 .setIcon(R.drawable.favorites)
		 .setShowAsAction(
		 MenuItem.SHOW_AS_ACTION_IF_ROOM);
		 
		 
		}
		 

		// subMenu.add("Logout").setIcon(R.drawable.logouticon);
		//subMenu.add("Go to Profile").setIcon(R.drawable.profile1);
		subMenu.add(0, 15, Menu.FIRST, "Add to Favorites").setIcon(R.drawable.favorites);
		subMenu.add(0, 10, Menu.NONE, "Logout").setIcon(R.drawable.logouticon);
		
		
		MenuItem subMenu1Item = subMenu.getItem();
		subMenu1Item.setIcon(R.drawable.ic_action_overflow);
		subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
			break;
		case 10:
			finish();
			HomeActivity.islogin = false;
			Intent intent_logout = new Intent(this, HomeActivity.class);
			intent_logout.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
			startActivity(intent_logout);
			break;
			
		case DASHBOARD:
			// finish();
			/*startActivity(new Intent(getApplicationContext(),
					HomeActivity.class));
			HomeActivity.islogin=false;*/
			break;
		
		case 15:
			//codes here
			ShowPropertyDetail details = (ShowPropertyDetail) getSupportFragmentManager()
			.findFragmentById(R.id.buyer_fragment);
			details.executeFavorites();
			
			
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CODE:

			if (resultCode == RESULT_OK) {
				if (data != null) {
					final Uri uri = data.getData();

					try {
						String Path = getRealPathFromURI(uri);
						EditMe details = (EditMe) getSupportFragmentManager()
								.findFragmentById(R.id.buyer_fragment);
						details.setUrl(Path);

					} catch (Exception e) {
						Log.e("FileSelectorTestActivity",
								"File select error CREATEACCOUNT", e);
					}
				}

			}
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public String getRealPathFromURI(Uri contentUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = getContentResolver().query(contentUri, proj, null,
				null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	public void onArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		ShowPropertyDetail newFragment = new ShowPropertyDetail();
		Bundle args = new Bundle();
		args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		args.putString("running", "favorites");
		newFragment.setArguments(args);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.buyer_fragment, newFragment);
		transaction.addToBackStack(null);

		transaction.commit();
	}

	public void SaleonArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		ShowPropertyDetail newFragment = new ShowPropertyDetail();
		Bundle args = new Bundle();
		args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		args.putString("running", "sale");
		newFragment.setArguments(args);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.buyer_fragment, newFragment);
		transaction.addToBackStack(null);

		transaction.commit();
	}

	public void RentArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		// ShowPropertyDetail

		Fragment newFragment = new ShowPropertyDetail();
		Bundle args = new Bundle();
		args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		args.putString("running", "rent");
		newFragment.setArguments(args);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.buyer_fragment, newFragment);
		transaction.addToBackStack(null);

		transaction.commit();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
	}
}

/*
 * @SuppressLint("ParserError") class MyTabsListener implements
 * ActionBar.TabListener { public Fragment fragment;
 * 
 * public MyTabsListener(Fragment fragment) { this.fragment = fragment; }
 * 
 * public void onTabReselected(Tab tab, FragmentTransaction ft) { //
 * Toast.makeText(ManageAgent.appContext, "Reselected!", //
 * Toast.LENGTH_LONG).show(); FragmentManager
 * fm=this.fragment.getFragmentManager(); fm.popBackStack();
 * 
 * }
 * 
 * public void onTabSelected(Tab tab, FragmentTransaction ft) {
 * ft.replace(R.id.buyer_fragment, fragment); }
 * 
 * public void onTabUnselected(Tab tab, FragmentTransaction ft) { //
 * ft.detach(fragment); }
 */

